package rad.framework.seam;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import rad.framework.validator.Required;
import rad.framework.validator.Unique;
import rad.framework.validator.UniqueProperties;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.NaturalId;

@Entity
@UniqueProperties( {
		@Unique(message = "Sample Entity name must be unique."),
		@Unique(property = "number", message = "Sample Entity {property} must be unique.") })
public class SampleEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	private Long id;
	private String name;
	private String number;
	private Date date;
	private BigDecimal bigDecimal;

	public enum Type {A, B};
	private Type type = Type.A;
	
	private RefEntity reference;
	
	private List<RefEntity> children;

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@NaturalId
	@Required
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

//	@NotNull
	public BigDecimal getBigDecimal() {
		return bigDecimal;
	}

	public void setBigDecimal(BigDecimal bigDecimal) {
		this.bigDecimal = bigDecimal;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	@Transient
	public boolean isA() {
		return Type.A.equals(getType());
	}

	@Transient
	public boolean isB() {
		return Type.B.equals(getType());
	}
	
	@Transient
	public String getSomething() {
		return "something";
	}
	
	public void setSomething(String s) {
	}
	
	@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.MERGE)
	public RefEntity getReference() {
		return reference;
	}

	public void setReference(RefEntity reference) {
		this.reference = reference;
	}

	@OneToMany(cascade=CascadeType.MERGE)
	public List<RefEntity> getChildren() {
		if (children == null) {
			children = new ArrayList<RefEntity>();
		}
		return children;
	}

	public void setChildren(List<RefEntity> children) {
		this.children = children;
	}
	
	public void addChild(RefEntity child) {
		getChildren().add(child);
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? super.hashCode() : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SampleEntity other = (SampleEntity) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
